package cn.czg.cms.entity.dto;

import cn.czg.cms.entity.Comment;
import cn.czg.cms.entity.User;
import cn.czg.cms.service.UserService;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

@Data
public class CommentDTO {
    private Long id;
    private String content;
    private String username;
    private String avatar;
    private LocalDateTime createdAt;
    private List<CommentDTO> replies;

    public static CommentDTO from(Comment comment, UserService userService) {
        CommentDTO dto = new CommentDTO();
        dto.setId(comment.getId());
        dto.setContent(comment.getContent());
        dto.setCreatedAt(comment.getCreatedAt());

        // 获取用户名、头像（可优化为缓存）
        User user = userService.getById(comment.getUserId());
        dto.setUsername(user.getNickname());
        dto.setAvatar(user.getAvatar());

        return dto;
    }
}
